package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.GouMaiKeChengActivity;
import com.xgkj.diyiketang.activity.MyZhiboListActivity;
import com.xgkj.diyiketang.activity.ZhiboPasswordActivity;
import com.xgkj.diyiketang.data.AdvertiseInfo;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.DialogUtil;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.ImageCycleView;
import com.xgkj.diyiketang.weidget.RoundImageView;
import com.xgkj.diyiketang.yizhibo.Constants;
import com.xgkj.diyiketang.yizhibo.PlayerNewActivity;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/31 0031.
 */

public class LiveListFirstPageAdapter extends BaseAdapter {
    private Context mContext;
    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();
    private ImageCycleView adView;
    private String currentLeibie;
    private String currentKemu;
    private String currentOrder;
    private String liveState;
    private String isPlayer;
    int type;
    private String isUse;
    private EatShowUserInfo info;

    public LiveListFirstPageAdapter(Context context) {
        mContext = context;

    }

    private List<AdvertiseInfo> adList = new ArrayList<AdvertiseInfo>();

    public void addImageView(List<AdvertiseInfo> list) {
        adList.clear();
        adList.addAll(list);
    }

    public void updateData(List<EatShowUserInfo> list, int s) {
        if (mList != null) {
            mList.addAll(list);
        }
        type = s;
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    private OnAdapterClickListener mAdapterListener;

    public void setOnAdapterClickListener(OnAdapterClickListener listener) {
        mAdapterListener = listener;
    }

    public interface OnAdapterClickListener {
        public void onItemLongClick(EatShowUserInfo info);
    }

    public void setCurrentLeibieType(String currentZhuboType) {
        currentLeibie = currentZhuboType;
    }

    public void setCurrentKemuType(String bType) {
        currentKemu = bType;
    }

    public void setCurrentOrder(String current) {
        currentOrder = current;
    }

    public ArrayList<EatShowUserInfo> getDataList() {
        return (ArrayList<EatShowUserInfo>) mList;
    }

    @Override
    public int getCount() {
//        if (mList != null) {
//            return mList.size();
//        }
//        return 0;
        if (mList.size() % 2 == 0) {
            return mList.size() / 2;
        }
        return mList.size() / 2 + 1;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LiveListFirstPageAdapter.ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new LiveListFirstPageAdapter.ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_zhibo_list, null);
            childHolder.address1 = (TextView) convertView.findViewById(R.id.zhibo_person_address);
            childHolder.address2 = (TextView) convertView.findViewById(R.id.zhibo_person_address_2);
            childHolder.neirong1 = (TextView) convertView.findViewById(R.id.zhibo_neirong);
            childHolder.neirong2 = (TextView) convertView.findViewById(R.id.zhibo_neirong_2);
            childHolder.num1 = (TextView) convertView.findViewById(R.id.zhibo_person_num);
            childHolder.num2 = (TextView) convertView.findViewById(R.id.zhibo_person_num_2);
            childHolder.ing1 = (TextView) convertView.findViewById(R.id.zhibo_ing);
            childHolder.ing2 = (TextView) convertView.findViewById(R.id.zhibo_ing_2);
            childHolder.person_name1 = (TextView) convertView.findViewById(R.id.zhibo_person_name);
            childHolder.person_name2 = (TextView) convertView.findViewById(R.id.zhibo_person_name_2);
            childHolder.person_state1 = (TextView) convertView.findViewById(R.id.zhibo_person_state);
            childHolder.person_state2 = (TextView) convertView.findViewById(R.id.zhibo_person_state_2);
            childHolder.zhibo_img = (RoundImageView) convertView.findViewById(R.id.zhibo_img);
            childHolder.zhibo_img2 = (RoundImageView) convertView.findViewById(R.id.zhibo_img2);
            convertView.setTag(childHolder);
        } else {
            childHolder = (LiveListFirstPageAdapter.ChildHolder) convertView.getTag();
        }
//        if (type==1||type==2){
//            mList.add(0, new EatShowUserInfo());
//        }
        final EatShowUserInfo info = mList.get(position * 2);
        if (info != null) {
            if (position == 0) {
                if (TextUtils.isEmpty(mList.get(0).getIsuse()) && TextUtils.isEmpty(mList.get(0).getLiveCoverUrl()) && TextUtils.isEmpty(mList.get(0).getLiveContent())) {
                    int screenWidth = ViewUtil.getScreenWidth(mContext);
                    ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(
                            childHolder.zhibo_img.getLayoutParams());
                    margin.setMargins(0, 0, 0, 0);// 在左边距100像素，顶边距100像素的位置显示图片
                    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(margin);
                    layoutParams.height = screenWidth / 2 - 10;// 设置图片的高度
                    layoutParams.width = screenWidth / 2 - 30; // 设置图片的宽度
                    childHolder.zhibo_img.setImageResource(R.drawable.bg_zhibo_first_x);
                    childHolder.zhibo_img.setLayoutParams(layoutParams);
//                childHolder.zhibo_img.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    childHolder.ing1.setVisibility(View.VISIBLE);
                    childHolder.num1.setVisibility(View.GONE);
                    childHolder.zhibo_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, MyZhiboListActivity.class);
                            intent.putExtra("type", "first_zhibo");
                            mContext.startActivity(intent);
                        }
                    });
                }else {
                    childHolder.ing1.setVisibility(View.GONE);
                    childHolder.num1.setVisibility(View.VISIBLE);
                }
            }else {
                childHolder.ing1.setVisibility(View.GONE);
                childHolder.num1.setVisibility(View.VISIBLE);
            }
            if (info.getNickName() != null) {
                try {
                    childHolder.person_name1.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (info.getArea() != null) {
                try {
                    childHolder.address1.setText(URLDecoder.decode(info.getArea(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            } else {
                childHolder.address1.setVisibility(View.GONE);
            }
            if (info.getLiveContent() != null) {
                try {
                    childHolder.neirong1.setText(URLDecoder.decode(info.getLiveContent(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (info.getViewCount() != null) {
                try {
                    childHolder.num1.setText(URLDecoder.decode(info.getViewCount(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            String fengmian = info.getLiveCoverUrl();
            if (fengmian != null && fengmian.length() > 0) {
//                ViewUtil.Size size = ViewUtil.getHeadImageSize_375(mContext);
                int screenWidth = ViewUtil.getScreenWidth(mContext);
//                ViewUtil.Size imageSize = ViewUtil.getHeadImageSize(mContext,screenWidth/4);
                ViewUtil.Size imageSize = new ViewUtil.Size(screenWidth / 2 - 10, screenWidth / 2 - 10);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(fengmian, imageSize));
                if (cacheBitmap != null) {
                    childHolder.zhibo_img.setImageBitmap(cacheBitmap);
                } else {
                    childHolder.zhibo_img.setImageResource(R.drawable.zhanweitu);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.zhibo_img, fengmian, imageSize);
                }
            }
            String state = info.getLiveState();
            String atime = info.getAppointTime();
            if (state != null && state.length() > 0) {
                if (state.equals("0")) {
//                    childHolder.ing1.setVisibility(View.VISIBLE);
//                    childHolder.ing1.setBackgroundResource(R.drawable.state_jinxingzhong);
                    childHolder.ing1.setText("直播中");
                } else if (state.equals("1")) {
//                    childHolder.ing1.setVisibility(View.VISIBLE);
//                    childHolder.ing1.setBackgroundResource(R.drawable.state_huifang);
                    childHolder.ing1.setText(R.string.play_back);
                } else if (state.equals("5")) {
//                    childHolder.ing1.setVisibility(View.VISIBLE);
//                    childHolder.ing1.setBackgroundResource(R.drawable.state_yugao);
                    childHolder.ing1.setText(atime + "小时后");
                }
                childHolder.zhibo_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toOtheract(info);
                    }
                });
                if (type == 0) {
                    childHolder.zhibo_img.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            DialogUtil.createDialog(mContext, -1, mContext.getString(R.string.prompt), mContext.getString(R.string.you_sure_delete_video), mContext.getString(R.string.ok), mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    if (mAdapterListener != null) {
                                        mAdapterListener.onItemLongClick(info);
                                    }
                                }
                            }, null);
                            return true;
                        }
                    });
                }
            }
        }
        if ((position * 2 + 1) < mList.size()) {
            final EatShowUserInfo info1 = mList.get(position * 2 + 1);
            String fengmian = info1.getLiveCoverUrl();
            if (fengmian != null && fengmian.length() > 0) {
                int screenWidth = ViewUtil.getScreenWidth(mContext);
//                    ViewUtil.Size imageSize = ViewUtil.getHeadImageSize(mContext,screenWidth/4);
                ViewUtil.Size imageSize = new ViewUtil.Size(screenWidth / 2 - 10, screenWidth / 2 - 10);
//                    ViewUtil.Size size = ViewUtil.getHeadImageSize_375(mContext);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(fengmian, imageSize));
                if (cacheBitmap != null) {
                    childHolder.zhibo_img2.setImageBitmap(cacheBitmap);
                } else {
                    childHolder.zhibo_img2.setImageResource(R.drawable.zhanweitu);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.zhibo_img2, fengmian, imageSize);
                }
            }
            String state = info1.getLiveState();
            String atime = info1.getAppointTime();
            if (state != null && state.length() > 0) {
                if (state.equals("0")) {
//                        childHolder.ing2.setVisibility(View.VISIBLE);
//                        childHolder.ing2.setBackgroundResource(R.drawable.state_jinxingzhong);
                    childHolder.ing2.setText("直播中");
                } else if (state.equals("1")) {
//                        childHolder.ing2.setVisibility(View.VISIBLE);
//                        childHolder.ing2.setBackgroundResource(R.drawable.state_huifang);
                    childHolder.ing2.setText(R.string.play_back);
                } else if (state.equals("5")) {
//                        childHolder.ing2.setVisibility(View.VISIBLE);
//                        childHolder.ing2.setBackgroundResource(R.drawable.state_yugao);
                    childHolder.ing2.setText(atime + "小时后");
                }
            }
            if (info1.getNickName() != null) {
                try {
                    childHolder.person_name2.setText(URLDecoder.decode(info1.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (info1.getArea() != null) {
                try {
                    childHolder.address2.setText(URLDecoder.decode(info1.getArea(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (info1.getLiveContent() != null) {
                try {
                    childHolder.neirong2.setText(URLDecoder.decode(info1.getLiveContent(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (info1.getViewCount() != null) {
                try {
                    childHolder.num2.setText(URLDecoder.decode(info1.getViewCount(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            childHolder.zhibo_img2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toOtheract(info1);
                }
            });
        }
        return convertView;
    }

    private void toOtheract(EatShowUserInfo liveinfo) {
        int liveType, vip, isCharge;
        String nickName, vid = "";
        if (!TextUtils.isEmpty(liveinfo.getIsMoive2())) {
            isPlayer = liveinfo.getIsMoive2();
        }
        if (!TextUtils.isEmpty(liveinfo.getIsuse())) {
            isUse = liveinfo.getIsMoive2();
        }
        if (TextUtils.isEmpty(liveinfo.getLiveState())) {
            return;
        } else {
            liveState = liveinfo.getLiveState();
        }

        if (liveState.equals("5")) {
            ToastUtil.showMessage(mContext, "预约暂未开始，敬请期待");
            return;
        }
        liveType = liveinfo.getLiveType();
        vip = liveinfo.isVip();
        isCharge = liveinfo.getIsCharge();

        String userId = null;
        final EatShowUserInfo userInfo = EatShowApp.getInstance().getmUserInfo();
        if (userInfo != null) {
            userId = userInfo.getUserId();
//                vip = userInfo.isVip();
        }
        String anchorId = liveinfo.getUserId();
        boolean isSelf = false;
        if (anchorId != null && anchorId.equals(userId)) {
            isSelf = true;
        }
        final boolean finalIsSelf = isSelf;
        Intent intent;
        if (finalIsSelf || liveType == 0 || (liveType == 1 && isCharge == 1)) {
            if (TextUtils.isEmpty(liveinfo.getVid())) {
                return;
            } else {
                vid = liveinfo.getVid();
            }

            intent = new Intent(mContext, PlayerNewActivity.class);
            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
            intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
            intent.putExtra("isUse", isUse);
            intent.putExtra("type", "liveFirst");
            intent.putExtra(Constants.LIVE_STATE, liveState);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } else if (liveType == 1 && isCharge == 0) {
            String liveTitle, fee;
            if (TextUtils.isEmpty(liveinfo.getLiveTitle()) && TextUtils.isEmpty(liveinfo.getNickName())
                    && TextUtils.isEmpty(liveinfo.getFee())) {
                return;
            } else {
                liveTitle = liveinfo.getLiveTitle();
                nickName = liveinfo.getNickName();
                fee = liveinfo.getFee();
            }
            //收费
            intent = new Intent(mContext, GouMaiKeChengActivity.class);
            intent.putExtra("From", 1);
            intent.putExtra("TITLE", liveTitle);
            intent.putExtra("ZUOZHE", nickName);
            intent.putExtra("FEE", fee);
            intent.putExtra("isUse", isUse);
            intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
            intent.putExtra(Constants.LIVE_STATE, liveState);
            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } else if (liveType == 2) {
            String headUrl;
            if (TextUtils.isEmpty(liveinfo.getNickName()) && TextUtils.isEmpty(liveinfo.getUserLogo())) {
                return;
            } else {
                nickName = liveinfo.getNickName();
                headUrl = liveinfo.getUserLogo();
            }
            if (TextUtils.isEmpty(liveinfo.getVid())) {
                return;
            } else {
                vid = liveinfo.getVid();
            }
            //密码
            intent = new Intent(mContext, ZhiboPasswordActivity.class);
            intent.putExtra("head_url", headUrl);
            intent.putExtra("nickname", nickName);
            intent.putExtra("isUse", isUse);
            intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
            intent.putExtra(Constants.LIVE_STATE, liveState);
            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } else if (liveType == 3 && vip >= 1) {
            if (TextUtils.isEmpty(liveinfo.getVid())) {
                return;
            } else {
                vid = liveinfo.getVid();
            }
            intent = new Intent(mContext, PlayerNewActivity.class);
            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
            intent.putExtra("type", "liveFirst");
            intent.putExtra("isUse", isUse);
            intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
            intent.putExtra(Constants.LIVE_STATE, liveState);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } else if (liveType == 4 && vip >= 2) {
            if (TextUtils.isEmpty(liveinfo.getVid())) {
                return;
            } else {
                vid = liveinfo.getVid();
            }
            intent = new Intent(mContext, PlayerNewActivity.class);
            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
            intent.putExtra("type", "liveFirst");
            intent.putExtra("isUse", isUse);
            intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
            intent.putExtra(Constants.LIVE_STATE, liveState);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } else if (liveType == 5 && vip >= 3) {
            if (TextUtils.isEmpty(liveinfo.getVid())) {
                return;
            } else {
                vid = liveinfo.getVid();
            }
            intent = new Intent(mContext, PlayerNewActivity.class);
            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
            intent.putExtra("type", "liveFirst");
            intent.putExtra("isUse", isUse);
            intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
            intent.putExtra(Constants.LIVE_STATE, liveState);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } else if (liveType == 6 && vip >= 4) {
            if (TextUtils.isEmpty(liveinfo.getVid())) {
                return;
            } else {
                vid = liveinfo.getVid();
            }
            intent = new Intent(mContext, PlayerNewActivity.class);
            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
            intent.putExtra("type", "liveFirst");
            intent.putExtra("isUse", isUse);
            intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
            intent.putExtra(Constants.LIVE_STATE, liveState);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } else if (liveType == 7 && vip >= 5) {
            if (TextUtils.isEmpty(liveinfo.getVid())) {
                return;
            } else {
                vid = liveinfo.getVid();
            }
            intent = new Intent(mContext, PlayerNewActivity.class);
            intent.putExtra("type", "liveFirst");
            intent.putExtra("isUse", isUse);
            intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
            intent.putExtra(Constants.LIVE_STATE, liveState);
            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } else if (liveType >= 3 && vip == 0) {
            Log.e("你的直播vip等级", vip + "vip等级");
            Toast.makeText(mContext, "请购买对应VIP等级后观看", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "请提升你的vip等级", Toast.LENGTH_SHORT).show();
        }
    }

    private View.OnClickListener mListener;

    public void setOnAdapterClickListener(View.OnClickListener listener) {
        mListener = listener;
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onClick(v);
            }
        }
    };

    class ChildHolder {
        //        SimpleDraweeView simpleDraweeView1;
//        SimpleDraweeView simpleDraweeView2;
        TextView address1, address2, neirong1, neirong2, num1, num2, ing1, ing2, person_name1, person_name2,
                person_state1, person_state2;
        RoundImageView zhibo_img, zhibo_img2, anchorImgView;
        TextView livegift;
        TextView liveman;
        TextView fansNum;
        TextView anchorNameText;
        TextView anchorSchoolText;
        ImageView fengmianImg;
        TextView liveTitle;
        TextView liveState;
        ImageButton charge_or_pwdText;
        TextView zhiboTimeText;
        TextView liveDesc;
    }

}
